package org.scboy.rbac.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.ResourceUserTypeService;
import org.scboy.rbac.service.UserTypeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("rbac/usertype")
@Menu("权限管理系统.用户类型管理")
public class UserTypeController {
	
	@Autowired
	UserTypeService userTypeService;

	@Autowired
	ResourceUserTypeService resourceUserTypeService;
	
	/**
	 * 分页查询用户类型列表
	 * 
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Permission("分页查询用户类型列表")
	public Result page(@RequestBody Model params) {
		Page page = userTypeService.page(params);
		return Result.Success(page.getData(), page.getCount());
	}

	/**
	 * 新增用户类型
	 * 
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增用户类型")
	public Result add(@RequestBody Model params) {
		if (userTypeService.add(params)) {
			return Result.Success("RBAC_USERTYPE_ADD_SUCCESS");
		} else {
			return Result.Error("RBAC_USERTYPE_ADD_ERROR");
		}
	}

	/**
	 * 更新用户类型
	 * 
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新用户类型")
	public Result update(@RequestBody Model params) {
		if (userTypeService.update(params)) {
			return Result.Success("RBAC_USERTYPE_UPDATE_SUCCESS");
		} else {
			return Result.Error("RBAC_USERTYPE_UPDATE_ERROR");
		}
	}

	/**
	 * 删除用户类型
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除用户类型")
	public Result delete(String id) {
		if (userTypeService.delete(id)) {
			return Result.Success("RBAC_USERTYPE_DELETE_SUCCESS");
		} else {
			return Result.Error("RBAC_USERTYPE_DELETE_ERROR");
		}
	}

	/**
	 * 启用禁用用户类型
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用用户类型")
	public Result checkState(String id) {
		if (userTypeService.checkState(id)) {
			return Result.Success("RBAC_USERTYPE_CHECKSTATE_SUCCESS");
		} else {
			return Result.Error("RBAC_USERTYPE_CHECKSTATE_ERROR");
		}
	}

	/**
	 * 获取启用的用户类型
	 * 
	 * @param request
	 * @return
	 */
	@PostMapping("list")
	@Permission("获取启用的用户类型")
	public Result list(@RequestBody Model params) {
		return Result.Success(userTypeService.list(params), "");

	}

	/**
	 * 获取用户类型信息
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取用户类型信息")
	public Result get(String id) {
		return Result.Success(userTypeService.get(id), "");
	}

	@PutMapping("sort")
	@Permission("升序降序")
	public Result sort(String id,int type) {
		boolean check=userTypeService.sort(id,type);
		if(check) {
			return Result.Success("RBAC_USER_TYPE_SORT_SUCCESS");
		}else {
			return Result.Error("RBAC_USER_TYPE_SORT_ERROR");
		}
	}
	
	/**
	 * 分页查询用户类型和资源关系列表
	 * @param request
	 * @return
	 */
	@PostMapping("rutpage")
	@Permission("分页查询用户类型和资源关系列表")
	public Result roleUserTypePage(@RequestBody Model params) {
		Page page = resourceUserTypeService.page(params);
		return Result.Success(page.getData(), page.getCount());
	}
	
	/**
	 * 新增用户类型和资源关系
	 * @param request
	 * @param userTypeId
	 * @param resId
	 * @return
	 */
	@PostMapping("addrut")
	@Permission("新增用户类型和资源关系")
	public Result addResourceUserType(String userTypeId,String[] resId) {
		if (resourceUserTypeService.addResource(userTypeId, resId)) {
			return Result.Success("RBAC_ROLE_USERTYPE_ADD_SUCCESS");
		} else {
			return Result.Error("RBAC_ROLE_USERTYPE_ADD_ERROR");
		}
	}

	/**
	 * 删除用户类型和资源关系
	 * @param request
	 * @param id
	 * @return
	 */
	@DeleteMapping("deleterut")
	@Permission("删除用户类型和资源关系")
	public Result deleteRoleUserType(String id) {
		if (resourceUserTypeService.delete(id)) {
			return Result.Success("RBAC_ROLE_USERTYPE_DELETE_SUCCESS");
		} else {
			return Result.Error("RBAC_ROLE_USERTYPE_DELETE_ERROR");
		}
	}
	
	/**
	 * 获取用户类型相关资源ID
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("getrut")
	@Permission("获取用户类型相关资源ID")
	public Result getResourceUserType(String userTypeId) {
		Model model=Model.create().add("resId", resourceUserTypeService.getResIds(userTypeId)).add("userTypeId", userTypeId);
		return Result.Success(model,"");
	}
}
